﻿
from PyQt4 import QtCore, QtGui
import mlt
from ui_mainwindow import Ui_MainWindow

class MainWindow(QtGui.QMainWindow):
	def __init__(self, parent = None):
		#initialisation de la fenetre
		QtGui.QDialog.__init__(self, parent)
		self._ui = Ui_MainWindow()
		self._ui.setupUi(self)
		self._ui.centralWidget.setAttribute(QtCore.Qt.WA_NativeWindow)

		#attributs
		self.m_producer = None
		self.m_consumer = None

		self._connectSlots()

	def _connectSlots(self):
		self.connect (self._ui.actionOpen, QtCore.SIGNAL("triggered()"), self.openVideo)
		self.connect (self._ui.actionPlay, QtCore.SIGNAL("triggered()"), self.play)
		self.connect (self._ui.actionPause, QtCore.SIGNAL("triggered()"), self.pause)
		self.connect (self._ui.actionSave, QtCore.SIGNAL("triggered()"), self.save)

	def initializeMlt(self):
		self._ui.statusBar.showMessage(self.tr("Loading plugins..."))

		mlt.Factory.init()
		self.m_profile = mlt.Profile("square_ntsc_wide")
		self.m_consumer = mlt.Consumer(self.m_profile, "sdl_preview")

		# Embed the SDL window in our GUI.
		self.m_consumer.set ("window_id", self._ui.centralWidget.winId())
		self.m_consumer.set ("play.audio_buffer", 4096)
		pal = QtGui.QPalette()
		self.m_consumer.set ("window_background", str(pal.color(QtGui.QPalette.Window).name())) # On aurait plus lui donner un nom de fichier qui serait le fichier de sorti. LEs options serait alors decritent dans profile, et les info de la video dans producer

		# Load a color producer to clear the video region with black.
		self.m_producer = mlt.Producer(self.m_profile, "color:") #Pour mettre une image blanche
		self.m_consumer.connect(self.m_producer)
		self.m_producer.set_speed (0.0)
		self.m_consumer.start()

		self._ui.statusBar.showMessage(self.tr("Ready"))

	def openVideo(self):
		fileName = QtGui.QFileDialog.getOpenFileName(self)
		if not fileName.isEmpty():
			producer = mlt.Producer(self.m_profile, str(fileName))
			if(producer.is_valid()):
				print "valid producer"
				print "in : ", producer.get_in(), ", out : ", producer.get_out()
				producer2 = producer.cut(100, 300)
				producer3 = producer.cut(900, 1300)
				producer4 = producer.cut(4000, 4300)
				print "after cut 1 : in : ", producer.get_in(), ", out : ", producer.get_out()
				print "after cut 2 : in : ", producer2.get_in(), ", out : ", producer2.get_out()
				temp = producer
				producer = producer2
				playlist = mlt.Playlist()
				playlist.append(producer2)
				playlist.append(producer3)
				self.m_consumer.stop()
				self.m_consumer.connect(playlist)
				del self.m_producer #useless ?
            			self.m_producer = producer;
				self.m_consumer.start()
				self.play()
		self.m_consumer.set ("refresh", 1);

	def play(self):
		self.m_producer.set_speed(1.0) # tres interessant !!
		self.m_consumer.set ("refresh", 1);
		self._ui.statusBar.showMessage(self.tr("Playing"))

	def pause(self):
		self.m_producer.set_speed(0)
		self._ui.statusBar.showMessage(self.tr("Paused"))

	def save(self):
		fileName = QtGui.QFileDialog.getOpenFileName(self)
		if not fileName.isEmpty():
			producer = mlt.Producer(self.m_profile, str(fileName))
			if(producer.is_valid()):
				producer2 = producer.cut(100, 300)
				producer3 = producer.cut(900, 1300)
				producer4 = producer.cut(4000, 4300)
				playlist = mlt.Playlist()
				playlist.append(producer2)
				playlist.append(producer3)
				playlist.append(producer4)
				cons = mlt.Consumer(mlt.Profile("avformat"), "avformat", "testEncode.avi")
				cons.stop()
				cons.connect(playlist)
				cons.start()
				playlist.set ("refresh", 1);
				print "ok c'est fini" # Je m'arrete la. Il faudrait quej e trouve pourquoi ca segmentation fault

	def resizeEvent(self, event):
		if(self.m_consumer):
			self.m_consumer.set ("refresh", 1);
